package cn.iocoder.yudao.module.promotion.service.prizedraw;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawAwardsRecordPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawAwardsRecordSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawAwardsRecordDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.prizedraw.PrizeDrawAwardsRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRIZE_DRAW_AWARDS_RECORD_NOT_EXISTS;

/**
 * 抽奖活动奖品记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PrizeDrawAwardsRecordServiceImpl implements PrizeDrawAwardsRecordService {

    @Resource
    private PrizeDrawAwardsRecordMapper prizeDrawAwardsRecordMapper;

    @Override
    public Long createPrizeDrawAwardsRecord(PrizeDrawAwardsRecordSaveReqVO createReqVO) {
        // 插入
        PrizeDrawAwardsRecordDO prizeDrawAwardsRecord = BeanUtils.toBean(createReqVO, PrizeDrawAwardsRecordDO.class);
        prizeDrawAwardsRecordMapper.insert(prizeDrawAwardsRecord);
        // 返回
        return prizeDrawAwardsRecord.getPrizeDrawAwardsRecordId();
    }

    @Override
    public void updatePrizeDrawAwardsRecord(PrizeDrawAwardsRecordSaveReqVO updateReqVO) {
        // 校验存在
        validatePrizeDrawAwardsRecordExists(updateReqVO.getPrizeDrawAwardsRecordId());
        // 更新
        PrizeDrawAwardsRecordDO updateObj = BeanUtils.toBean(updateReqVO, PrizeDrawAwardsRecordDO.class);
        prizeDrawAwardsRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePrizeDrawAwardsRecord(Long id) {
        // 校验存在
        validatePrizeDrawAwardsRecordExists(id);
        // 删除
        prizeDrawAwardsRecordMapper.deleteById(id);
    }

    @Override
        public void deletePrizeDrawAwardsRecordListByIds(List<Long> ids) {
        // 校验存在
        validatePrizeDrawAwardsRecordExists(ids);
        // 删除
        prizeDrawAwardsRecordMapper.deleteByIds(ids);
        }

    private void validatePrizeDrawAwardsRecordExists(List<Long> ids) {
        List<PrizeDrawAwardsRecordDO> list = prizeDrawAwardsRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRIZE_DRAW_AWARDS_RECORD_NOT_EXISTS);
        }
    }

    private void validatePrizeDrawAwardsRecordExists(Long id) {
        if (prizeDrawAwardsRecordMapper.selectById(id) == null) {
            throw exception(PRIZE_DRAW_AWARDS_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public PrizeDrawAwardsRecordDO getPrizeDrawAwardsRecord(Long id) {
        return prizeDrawAwardsRecordMapper.selectById(id);
    }

    @Override
    public PageResult<PrizeDrawAwardsRecordDO> getPrizeDrawAwardsRecordPage(PrizeDrawAwardsRecordPageReqVO pageReqVO) {
        return prizeDrawAwardsRecordMapper.selectPage(pageReqVO);
    }

}